﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace eCompany.Extented_Controls
{
    public partial class LabelExtended : UserControl
    {
        [Browsable(true)]
        public new event EventHandler TextChanged;
    
        public delegate bool ValidateValueHandler(string str);
        public ValidateValueHandler ValidateValue = null;

        public delegate string ReformatValueHandler(string str);
        public ReformatValueHandler ReformatValue = null;

        private string m_str;
        private string m_ext;

        public LabelExtended()
        {
            InitializeComponent();
        }

        [Browsable(true)]
        public bool Locked { get; set; }

        [Browsable(true)]
        public ContentAlignment TextAlign
        {
            get
            {
                return _label.TextAlign;
            }
            set
            {
                _label.TextAlign = value;
            }
        }

        [Browsable(true)]
        public Color LabelForeColor
        { 
            get
            {
                return _label.ForeColor;
            }
            set
            {
                _label.ForeColor = value;
            }
        }

        [Browsable(true)]
        public Color LabelBackColor
        { 
            get
            {
                return _label.BackColor;
            }
            set
            {
                _label.BackColor = value;
                this.BackColor = value;
                //_textBox.BackColor = value;
            }
        }

        [Browsable(true)]
        public string TextExtension
        {
            get
            {
                return m_ext;
            }
            set
            {
                m_ext = value;
            }
        }

        [Browsable(true)]
        public override Font Font
        {
            get
            {
                return base.Font;
            }
            set
            {
                base.Font = value;
                _label.Font = value;
                _textBox.Font = value;

                float desiredheight = value.GetHeight() + 7;
                this.Height = (int)(desiredheight)+1;
            }
        }

        [Browsable(true)]
        public string TextLabel
        { 
            get
            {
                return m_str;
            }
            set
            {
                m_str = value;
                _label.Text = m_str+m_ext;
                _textBox.Text = m_str;
            }
        }

        private void _label_DoubleClick(object sender, EventArgs e)
        {
            if (Locked == true)
                return;
            _label.Visible = false;
            _textBox.Visible = true;
            _textBox.Focus();
        }

        private void _textBox_Leave(object sender, EventArgs e)
        {
            _textBox.Visible = false;
            _label.Visible = true;
        }

        private void _textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                _textBox.Visible = false;
                if (ReformatValue != null)
                    _textBox.Text = ReformatValue(_textBox.Text);
                this.m_str = _textBox.Text;
                _label.Text = m_str+m_ext;
                _label.Visible = true;
                if (TextChanged != null)
                {
                    TextChanged(this, new EventArgs());
                }
            }
            else if (e.KeyChar == 27) //ESC
            {
                _textBox.Visible = false;
                _textBox.Text = m_str;
                this.m_str = _textBox.Text;
                _label.Visible = true;
            }
            else if (e.KeyChar == '\b') //Backspace
            {
                return;
            }
            else if (ValidateValue != null && ValidateValue(_textBox.Text + e.KeyChar) == false)
            {
                e.Handled = true;
                return;
            }
        }
    }
}
